package cn.xiu.drugstore.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.xiu.drugstore.dao.DrugDao;
import cn.xiu.drugstore.pojo.Drug;
import cn.xiu.drugstore.util.BaseDao;
/**
 * 药品实现类
 * @author 贾
 *
 */
public class DrugDaoImpl extends BaseDao implements DrugDao {
	Drug drug=null;
	/**
	 * 添加药品
	 */
	@Override
	public int addDrug(Drug drug) throws Exception {
		String sql="INSERT INTO drug(drug_name,drug_price,drug_count,drug_typeid,drug_agency) VALUE(?,?,?,?,?) ";
		Object[] params={drug.getDrugName(),drug.getDrugPrice(),drug.getDrugCount(),drug.getDrugTypeid(),drug.getDrugAgency()};
		return super.executeUpdate(sql, params);
	}
	/**
	 * 查找所有药品
	 */
	@Override
	public List<Drug> findAllDrug() throws Exception {
		ArrayList<Drug> list =new ArrayList<Drug>();
		String sql="SELECT drug_name,drug_price,drug_count,drug_typeid,drug_agency FROM drug ORDER BY drug_id DESC";
		Object[] params={};
		ResultSet rs=super.executeQuery(sql, params);
		while(rs!=null&&rs.next()){
			drug=new Drug();
			drug.setDrugName(rs.getString("drug_name"));
			drug.setDrugPrice(rs.getDouble("drug_price"));
			drug.setDrugCount(rs.getInt("drug_count"));
			drug.setDrugTypeid(rs.getInt("drug_typeid"));
			drug.setDrugAgency(rs.getString("drug_agency"));
			list.add(drug);
		}
		super.closeResource(rs);
		return list;
	}
	/**
	 * 根据药品名查询药品
	 */
	@Override
	public Drug queryByDrugName(String drugName) throws Exception {
		String sql="SELECT drug_id,drug_name,drug_price,drug_count,drug_typeid,drug_agency FROM drug WHERE drug_name=?";
		Object[] params={drugName};
		ResultSet rs=super.executeQuery(sql, params);
		if(rs.next()){
			drug=new Drug();
			drug.setDrugId(rs.getInt("drug_id"));
			drug.setDrugName(rs.getString("drug_name"));
			drug.setDrugPrice(rs.getDouble("drug_price"));
			drug.setDrugCount(rs.getInt("drug_count"));
			drug.setDrugTypeid(rs.getInt("drug_typeid"));
			drug.setDrugAgency(rs.getString("drug_agency"));
		}
		super.closeResource(rs);
		return drug;
	}
	/**
	 * 根据id修改药品价格
	 */
	@Override
	public int updateDrug(Double drugPrice, Integer drugId) throws Exception {
		String sql="UPDATE drug SET drug_price=? WHERE drug_id=?";
		Object[] params={drugPrice,drugId};
		return super.executeUpdate(sql, params);
		
	}
	/**
	 * 根据id查询药品
	 */
	@Override
	public Drug queryByDrugId(String drugId) throws Exception {
		String sql="SELECT drug_id,drug_name,drug_price,drug_count,drug_typeid,drug_agency FROM drug WHERE drug_id=?";
		Object[] params={drugId};
		ResultSet rs=super.executeQuery(sql, params);
		if(rs.next()){
			drug=new Drug();
			drug.setDrugId(rs.getInt("drug_id"));
			drug.setDrugName(rs.getString("drug_name"));
			drug.setDrugPrice(rs.getDouble("drug_price"));
			drug.setDrugCount(rs.getInt("drug_count"));
			drug.setDrugTypeid(rs.getInt("drug_typeid"));
			drug.setDrugAgency(rs.getString("drug_agency"));
		}
		super.closeResource(rs);
		return drug;
	}
	/**
	 * 根据id修改药品价格
	 */
	@Override
	public int updateDrug(Integer drugCount, Integer drugId) throws Exception {
		String sql="UPDATE drug SET drug_count=? WHERE drug_id=?";
		Object[] params={drugCount,drugId};
		return super.executeUpdate(sql, params);
	}
	@Override
	public int deleteDrug(String drugId) throws Exception {
		String sql="DELETE FROM drug WHERE drug_id=?";
		Object[] params={drugId};
		return super.executeUpdate(sql, params);
	}

}
